package com.example.demo.controller;

import com.example.demo.model.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.*;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName: ParamController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2023/10/21 12:05
 * @Version 1.0
 */
//类路径可加可不加, 但是企业开发是要加
    //因为: 1. 避免重复. 2. 方便查找代码.
@RequestMapping("/param")
@RestController
public class ParamController {
    /**
     * 传递单个参数
     * @param name
     * @return
     */
    @RequestMapping("/m1")//方法路径和方法名不一定要相同
    public String m1(String name) {
        return "name = " + name;
    }

    /**
     * 传递多个参数, 直接发送, 参数的顺序无所谓
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "name = " + name + "; age = " + age;
    }

    /**
     * 建议使用包装类型, 因为如果不传值, 包装类可以直接接收null, 但是基本类型不能接收null
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/m3")
    public String m3(String name, int age) {
        return "name = " + name + "; age = " + age;
    }

    //如果直接使用参数, 那传递不同参数就需要时常修改方法, 很麻烦

    /**
     * 可以把参数封装成对象, 直接传递对象.
     * @param person
     * @return
     */
    @RequestMapping("/m4")
    public String m4(Person person) {
        return "person:" + person.toString();
    }

    /**
     * 后端参数重命名
     * @RequestParam("前端参数") 参数类型 重命名的名字
     * 重命名后的参数是必须传的
     * 如果不想必传, 那么就将required置成false
     * @RequestParam(value = "前端参数名称", required = false)
     * @param username
     * @return
     */
    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name", required = false) String username) {
        return "name: " + username;
    }

    /**
     * 传递数组, 当同一个参数有多个时, 会自动封装成数组
     * @param arr
     * @return
     */
    @RequestMapping("/m6")
    public String m6(String[] arr) {
        return Arrays.toString(arr);
    }

    /**
     * 传递集合,
     * @param list
     * @return
     */
    @RequestMapping("/m7")
    public String m7(@RequestParam List<String> list) {
        return list + " 长度:" + list.size();
    }

    /**
     * 传递Json必须加上注解@RequestBody
     * @param person
     * @return
     */
    @RequestMapping("/m8")
    public String m8(@RequestBody Person person) {
        return person.toString();
    }
    @RequestMapping("/m9/{userId}/{name}")
    public String m9(@PathVariable Integer userId, @PathVariable("name") String username) {
        return "" + userId + " " + username;
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "success";
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie :
//                cookies) {
//            System.out.println(cookie.getName() + cookie.getValue());
//        }
        if(cookies != null) {
            Arrays.stream(cookies).forEach(cookie->{
                System.out.println(cookie.getName() + cookie.getValue());
            });
        }
        return "success";
    }
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String dw) {

        return "success: " + dw;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username", "zhangsan");
        return "success";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession();//默认值为true, 如果没有session, 会创建一个
        if(session != null) {
            String username = (String) session.getAttribute("username");
            return "登录用户:"+username;
        }
        return "session为空";
    }
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String username) {
        return username;
    }
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session) {//等同于HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        return "登录用户:"+username;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }
}
